<template>
  <div class="category-selector">
    <!-- 主输入框（严格匹配图像参数） -->
    <div 
      class="selector-box"
      :class="{ active: isOpen }"
      :style="{ 
        borderColor: isOpen ? '#1890ff' : inputBorderColor,
        borderRadius: isOpen ? '4px 4px 0 0' : '4px'
      }"
      @click="toggleDropdown"
    >
      <span class="placeholder">{{ selectedCategory || '请选择应用分类' }}</span>
      <span class="arrow"></span>
    </div>

    <!-- 下拉列表（带审核状态支持） -->
    <transition name="dropdown">
      <ul 
        v-show="isOpen"
        class="dropdown-list"
        :style="{ 
          borderColor: isError ? '#ff4d4f' : '#1890ff',
          maxHeight: categories.length > 12 ? '400px' : '240px'
        }"
      >
        <li
          v-for="(item, index) in categories"
          :key="item.id || index"
          class="option-item"
          :class="{
            selected: item.label === selectedCategory,
            rejected: item.status === 'rejected'
          }"
          @click="handleSelect(item)"
        >
          <span class="text">{{ item.label }}</span>
          <span 
            v-if="item.status === 'rejected'"
            class="reject-tag"
          >
            <DeepinIcon name="alert-circle" size="12" />
            审核未通过
          </span>
        </li>
      </ul>
    </transition>
  </div>
</template>

<script setup>
import { ref, computed, onMounted } from 'vue'

const props = defineProps({
  modelValue: String,
  categories: {
    type: Array,
    default: () => []
  }
})

const emit = defineEmits(['update:modelValue'])

// 响应式数据
const isOpen = ref(false)
const localCategories = ref([...props.categories])
const selectedCategory = ref(props.modelValue)

// 动态样式计算
const inputBorderColor = computed(() => 
  selectedCategory.value ? '#1890ff' : '#e8e8e8'
)

const isError = computed(() => 
  localCategories.value.some(item => item.status === 'rejected')
)

// 数据加载（兼容props传入和独立加载）
onMounted(async () => {
  if (props.categories.length === 0) {
    try {
      const response = await fetch('/categories.json')
      localCategories.value = await response.json()
    } catch (error) {
      console.error('分类数据加载失败:', error)
    }
  }
})

// 事件处理
const toggleDropdown = () => {
  isOpen.value = !isOpen.value
}

const handleSelect = (item) => {
  selectedCategory.value = item.label
  isOpen.value = false
  emit('update:modelValue', item.label)
}
</script>

<style scoped>
/* 严格匹配图像设计参数 */
.category-selector {
  width: 320px;
  position: relative;
}

.selector-box {
  border: 1px solid;
  padding: 8px 12px;
  cursor: pointer;
  transition: all 0.2s cubic-bezier(0.4, 0, 0.2, 1);
  background: #ffffff;
}

.dropdown-list {
  border: 1px solid;
  margin: 0;
  padding: 0;
  background: #ffffff;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  overflow-y: auto;
}

.option-item {
  height: 40px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 12px;
  border-bottom: 1px solid #f0f0f0;
  color: #333333;
  transition: background-color 0.2s;
}

.option-item:hover {
  background-color: #f5f5f5;
}

.reject-tag {
  color: #ff4d4f;
  font-size: 12px;
  display: inline-flex;
  align-items: center;
  gap: 4px;
}

/* 精确动画参数 */
.dropdown-enter-active {
  transition: all 0.2s ease;
  transform-origin: top center;
}

.dropdown-leave-active {
  transition: all 0.2s cubic-bezier(0.4, 0, 0.2, 1);
}

.dropdown-enter-from,
.dropdown-leave-to {
  opacity: 0;
  transform: translateY(-8px);
}
</style>